#!/bin/bash

#=======================================================================
# pakk_logg
# File ID: 7bd794f8-5551-11e0-942f-b7ae6f27caa2
# Compress script logs
# License: GNU General Public License version 2 or later.
#=======================================================================

lockdir=~/.pakk_log.lock
mkdir "$lockdir" || { echo $0: $lockdir: Cannot create lockdir >&2; exit 1; }

myexit() {
    rmdir "$lockdir" || { echo $0: $lockdir: Cannot remove lockdir >&2; exit 1; }
    exit $1
}
trap "myexit 1" INT TERM

cd ~/log/script || { echo $0: ~/log/script: Unable to chdir >&2; exit 1; }
find -type f | grep -E '\.(scrlog|timing)$' | while read f; do
    uuid=$(echo "$f" | finduuid)
    ps auxw | grep -v "grep $uuid" | grep $uuid | grep -q . || gzip -v "$f"
done

ircdir=$HOME/log/irc

if test -d "$ircdir/."; then
    cd "$ircdir" || { echo $0: $ircdir: Unable to chdir >&2; exit 1; }
    currdate=`date +"%Y-%m-%d"`
    find -type f -name '*.log' -print0 | grep -vz "$currdate" | xargs -0r gzip -v
fi

dfdir=$HOME/log/df

if test -d "$dfdir/."; then
    cd "$dfdir" || { echo $0: $dfdir: Unable to chdir >&2; exit 1; }
    currdate=`date -u +"%Y-%m-%d"`
    find -type f -name '*.log' -print0 | grep -vz "$currdate" | xargs -0r gzip -v
fi

myexit 0
